//author:z.En | wzy@z-en.com

	var $JEVENT={
//		eventTypeList:{click:true},
		paramJson : false,
		xmlHttpRequest:false,
		sender:false,
		callBack:false,
		dataElement:false,
		data:{},
		_sendEvent : function(param){
			this._init();
			this.paramJson = param;
			this._initEvent(this.paramJson.cb);
			

			var url = this.sender.getActionUrl();
			sendType = this.paramJson.st+'';
			switch(sendType.toLowerCase()){
				case 'get':
					this._getAjax(url);
					break;
				case 'xml':
					this._xmlAjax(url);
					break;
				default:
					this._postAjax(url);
			}



		},
		_init:function(){
			this.sender = this._getSender();
		},
		_initEvent:function(callBack){
			//获得页面所有对象，并且绑定
			for(var i in document.all){
				document.all[i].isArray = this.isArray;
				document.all[i].getValue = this.getValue;
			}


			if(!!callBack){
				this.callBack = callBack			
			}
			var dataElementJson = '';
			for(var key in this.paramJson){

				if(key != 'cb' && key != 'st'){

					dataElementJson += '"'+key+'":';

					tmpElement = this._getElement(this.paramJson[key]);


					
					dataElementJson += tmpElement.getValue();

					dataElementJson += ',';

				}
			}

			dataElementJson = dataElementJson.substr(0,dataElementJson.length-1);
			dataElementJson = '{'+dataElementJson+'}';
			
			this.data.json = this._toJson(dataElementJson);
			this.data.text = this._json2text(dataElementJson);
			this.data.xml = this._json2xml(dataElementJson);



			this.sender.getAllAttrStr = this.getAllAttrStr;
			this.sender.getAllAttrXml = this.getAllAttrXml;
			this.sender.getActionUrl = this.getActionUrl;
			/*
			switch(event.type){
				case 'click':
//					this.onclick();
					break;
				case 'change':
					

			}
			*/
		},
		_toJson:function(val){
				try{
					json = JSON.parse(val);
				}catch(err){
					return {};

				}
				return json;
		},
		_addEventList:function(eventName,eventFunc){
			this.sender.addEventListener(eventName,eventFunc);
		},

		_getXmlHttpRequest:function(){
			if(!this.xmlHttpRequest){
			
			

				var xmlHttp = false;

				try {
				  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				  try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				  } catch (e2) {
					xmlHttp = false;
				  }
				}

				if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
				  xmlHttp = new XMLHttpRequest();
				}
				this.xmlHttpRequest = xmlHttp;
			}
			return this.xmlHttpRequest;
		
		},
		_getEvent:function(){
			try{
				ret_event = event;

			}catch(err){
				tmpSender = this._getSender.caller;
				prevSender = null;
//					while(Object.prototype.toString.apply(tmpSender)==='[object Function]'){
				while(!!tmpSender){
					//如果为空
					prevSender = tmpSender;

					tmpSender = tmpSender.caller;
				}

//					this.sender = this._getSender.caller.caller.caller.arguments[0].target;
				ret_event = prevSender.arguments[0];
			}
			return ret_event;
		
		},
		_getSender:function()
		{



			if(!!this._getEvent().srcElement)
			{
				sender = this._getEvent().srcElement;
			}
			else
			{
				sender = this._getEvent().target;
			}

			return sender;
		},
		_getAjax:function(url){

			if(url.indexOf('?')<0)
			{
				url += '?';
			}else
			{
				url += '&';
			}
			url += this.sender.getAllAttrStr()+this.data.text;
			
			
			var xmlHttpRequest = this._getXmlHttpRequest();
			var callBackFunc = this.callBack;
			xmlHttpRequest.open('GET',url,true);
			var outThis = this;
			xmlHttpRequest.onreadystatechange = function(){
				
				if(xmlHttpRequest.readyState == 4){
					var resText = xmlHttpRequest.responseText;
					var resJson = outThis._toJson(resText);
					var res = {};
					res.text = resText;
					res.json = resJson
					callBackFunc(res);
				}					
			};
				

			xmlHttpRequest.send();
		},
		_xmlAjax:function(url){


			var xmlHttpRequest = this._getXmlHttpRequest();
			var callBackFunc = this.callBack;
			xmlHttpRequest.open('POST',url,true);
			var outThis = this;
			xmlHttpRequest.onreadystatechange = function(){
				
				if(xmlHttpRequest.readyState == 4){
					var resText = xmlHttpRequest.responseText;
					var resJson = outThis._toJson(resText);
					var res = {};
					res.text = resText;
					res.json = resJson
					callBackFunc(res);
				}
			};
			if(!!this.data){
				xmlString = this.data.xml;
			}else{
				xmlString = this.sender.getAllAttrXml();
			}

			xmlHttpRequest.setRequestHeader("Content-Type", "text/xml");

			xmlHttpRequest.send(xmlString);
		},
		_postAjax:function(url){

			var xmlHttpRequest = this._getXmlHttpRequest();
			var callBackFunc = this.callBack
			xmlHttpRequest.open('POST',url,true);
			var outThis = this;
			//绑定回调函数
			xmlHttpRequest.onreadystatechange = function(){
				
				if(xmlHttpRequest.readyState == 4){
					var resText = xmlHttpRequest.responseText;
					var resJson = outThis._toJson(resText);
					var res = {};
					res.text = resText;
					res.json = resJson;
					callBackFunc(res);
				}
			};
//			var xmlString = this.sender.getAllAttrXml();
			xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  

			xmlHttpRequest.send(this.sender.getAllAttrStr()+this.data.text);
		},



		_getElement:function(elemStr){
			splitArr = elemStr.split("=");

			switch(splitArr[0]){
				case 'id':
					element = document.getElementById(splitArr[1]);
					break;
				case 'name':

					element = document.getElementsByName(splitArr[1]);

					break;
				default:
					element = document.getElementById(splitArr[1]);
			}
			element.isArray = this.isArray;
			element.getValue = this.getValue;
			return element;
		},
		_getValue:function(obj){
			return this.getValue(obj);
		},
		_isArray:function(obj){
			objType = Object.prototype.toString.apply(obj);
			if(objType === '[object NodeList]' || objType === '[object Array]' || objType === '[object HTMLCollection]'){
				return true;
			}
			return false;
		},
		_json2text:function(json){

			json = this._toJson(json);

			ret = '';
			for(var i in json){

				if(this._isArray(json[i])){
					for(j in json[i]){
						ret += i+'['+j+']='+escape(json[i][j])+'&';
					}
				}else{
					ret += i+'='+escape(json[i])+'&';
				}
			}

			return ret;
		},
		_json2xml:function(json){
			var xml = '<profile>';
			
			for(i in json){
				xml += '<'+i+'>'+
					escape(json[i])+
					'</'+i+'>';
			}
			xml += '</profile>';
			return xml;
		},
		getValue:function(obj){
			if(!!!obj){
				obj = this;
			}

			returnStr = '[';


			if(obj.isArray && obj.isArray()){


				//只循环元素的个数次，否则数组内部的函数也将被遍历出来
				for(var i = 0;i<obj.length;i++){
					returnStr += '"'+obj[i].value+'",';
				}
				returnStr = returnStr.substr(0,returnStr.length-1);

				return returnStr+']';
			}
			return '"'+obj.value+'"';
		},
		isArray:function(){
			objType = Object.prototype.toString.apply(this);
			if(objType === '[object NodeList]' || objType === '[object Array]' || objType === '[object HTMLCollection]'){
				return true;
			}
			return false;
		},
			//获得该对象的所有参数的字符串形式
		getAllAttrStr:function(){
			var param = '';

			var attrAll = this.attributes;
			var attrLen = attrAll.length;
			for(var i=0;i<attrLen;i++){
				if(!!(this.getAttribute(attrAll[i].name))){
					param += attrAll[i].name + '=' + escape(attrAll[i].value) + '&';
				}
			}
			return param;
		},
		getAllAttrXml:function(){
			var xml = '<profile>';

			var attrAll = this.attributes;
			var attrLen = attrAll.length;
			for(var i=0;i<attrLen;i++){
				xml += '<'+attrAll[i].name+'>' +
					escape(attrAll[i].value) +
					'<'+attrAll[i].name+'>';

			}
			xml += '</profile>';
			return xml;
		},
			//获得该对象的ACTION URL地址
		getActionUrl:function(){
			var actionUrl = '';
//			var sender = this._getSender();
			var parentNode = this.parentNode;
			while(parentNode!=null){
				if(!!parentNode.action){
					actionUrl = parentNode.action;
					break;
				}
				parentNode = parentNode.parentNode;
			}

			if(actionUrl == ''){
				actionUrl = window.location.href;
			}

			return actionUrl;
		}
	
	
	};